package com.contexttree.utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;

/**
 * This class implements a print output on file
 * Politecnico di Milano 2012
 * @author Paolo Barbaglia
 * @author Filippo Ferrari
 *
 */
public class FilePrinter {

	public static String printResultQueryOnFile(String filename, String path, ArrayList<String> resultSetPos){
		String fullpath = path + "//" + filename;
		try{
			PrintStream output = new PrintStream(new FileOutputStream(fullpath));
			for (int i=0; i < resultSetPos.size(); i++) {
				output.print(resultSetPos.get(i));
				output.print("\n");
			}
			output.close();
		}
		catch(FileNotFoundException ex) {
			Printer.print(ex);
		}
	return fullpath;
	}
	
	
	public static void printDictionary(String filename, String path, String[] association){
		String fullpath = path + "//" + filename;
		int i=0;
		
		try{
			PrintStream output = new PrintStream(new FileOutputStream(fullpath));
			while (!association[i].equals("")) {
				output.print(i+1);
				output.print("->");
				output.print(association[i]);
				output.print("\n");
				i++;
			}
			output.close();
		}
		catch(FileNotFoundException ex) {
			Printer.print(ex);
		}
	}
	
	public static void printTable(String filename, String path, String[][] table){
		String fullpath = path + "//" + filename;
		
		try{
			PrintStream output = new PrintStream(new FileOutputStream(fullpath));
			for (int i=0; i<table.length; i++){
				for (int j=0; j<table[0].length; j++){
					output.print(table[i][j]+",");
				}
			output.print("\n");
			}
			output.close();
		}
		catch(FileNotFoundException ex) {
			Printer.print(ex);
		}
	}
	
	public static void printViews(String filename, String path, String[][] table){
		String fullpath = path + "//" + filename;
		
		try{
			PrintStream output = new PrintStream(new FileOutputStream(fullpath));
			for (int i=0; i<table.length; i++){
				table [i][0] = "View:" + table [i][0];
				table [i][1] = "Records.Covered:" + table [i][1];
				table [i][2] = "Cover.Percent:" + table [i][2];
				output.println(table[i][0] + "," + table[i][1] + "," + table [i][2]);
			}
			output.close();
		}
		catch(FileNotFoundException ex) {
			Printer.print(ex);
		}
	}
	
	public static void printTableConverted(String filename, String path, String[][] converted){
		String fullpath = path + "//" + filename;
		
		try{
			PrintStream output = new PrintStream(new FileOutputStream(fullpath));
			for (int i=0; i<converted.length; i++){
				for (int j=0; j<converted[0].length; j++){
					output.print(converted[i][j]+" ");
				}
			output.print("\n");
			}
			output.close();
		}
		catch(FileNotFoundException ex) {
			Printer.print(ex);
		}
	}
}